﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace XmlStudio.Resources.UserControls {
    /// <summary>
    /// Interaction logic for OKCancelButtons.xaml
    /// </summary>
    public partial class OKCancelButtons : UserControl {
        public OKCancelButtons() {
            InitializeComponent();
        }

        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(RoutedCommand), typeof(OKCancelButtons), new PropertyMetadata(null));
        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(RoutedCommand), typeof(OKCancelButtons), new PropertyMetadata(null));
        public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(RoutedCommand), typeof(OKCancelButtons), new PropertyMetadata(null));

        public RoutedCommand Command {
            get {
                return GetValue(CommandProperty) as RoutedCommand;
            }

            set {
                SetValue(CommandProperty, value);
            }
        }

        public object CommandParameter {
            get {
                return GetValue(CommandParameterProperty);
            }

            set {
                SetValue(CommandParameterProperty, value);
            }
        }

        public IInputElement CommandTarget {
            get {
                return GetValue(CommandTargetProperty) as IInputElement;
            }

            set {
                SetValue(CommandTargetProperty, value);
            }
        }

        public event EventHandler<RoutedEventArgs> OKClicked;

        public event EventHandler<RoutedEventArgs> CancelClicked;

        private void OKButtonClicked(object sender, RoutedEventArgs e) {
            this.OnOKClicked(e);

            if(Command != null && Command.CanExecute(this.CommandParameter, this.CommandTarget)) {
                Command.Execute(this.CommandParameter, this.CommandTarget);
            }
        }

        private void CancelButtonClicked(object sender, RoutedEventArgs e) {
            this.OnCancelClicked(e);
        }

        private void OnOKClicked(RoutedEventArgs e) {
            var okClicked = this.OKClicked;
            if(okClicked != null) {
                okClicked(this, e);
            }
        }

        private void OnCancelClicked(RoutedEventArgs e) {
            var cancelClicked = this.CancelClicked;
            if(cancelClicked != null) {
                cancelClicked(this, e);
            }
        }
    }
}
